﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AlexandriaPressUserBusinessLayer;
using AlexandriaPressUserObject;

namespace AlexandriaPressWebsite.MasterPages.UserPages
{
    public partial class EditAccountPage : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            UserBusinsessLayer business = new UserBusinsessLayer();
            string _UserType = business.getUserType(HttpContext.Current.User.Identity.Name);
            if (_UserType == "Type_Buyer" || _UserType == "Type_Admin" || _UserType == "Type_ProofReader" || _UserType == "Type_Author")
            {

            }
            else
            {
                Response.Redirect("~/MasterPages/ErrorPage.aspx");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
                if (Session["blocked"] != null)
                {
                    Session.RemoveAll();
                    Response.Redirect("~/MasterPages/MainPage.aspx");
                }

                if (!Page.IsPostBack)
                {
                    UserBusinsessLayer business = new UserBusinsessLayer();
                    User user = business.getUser(HttpContext.Current.User.Identity.Name);
                    Email.Text = user.Email.ToString();
                    DeliveryAdd.Text = user.DeliveryAdd.ToString();
                    LastName.Text = user.LastName.ToString();
                    FirstName.Text = user.FirstName.ToString();
                }

        }

        protected void saveAccountButton_Click(object sender, EventArgs e)
        {
            UserBusinsessLayer business = new UserBusinsessLayer();
            User user = business.getUser(HttpContext.Current.User.Identity.Name);

            if(Email.Text != user.Email.ToString())
            {
                int emailCount = business.uniqueEmail(Email.Text);

                if (emailCount > 0)
                    emailExists.Visible = true;
                else
                {
                    emailExists.Visible = false;
                }
            }
            else
            {
                try
                {
                    UserBusinsessLayer.updateAccount(HttpContext.Current.User.Identity.Name, LastName.Text, FirstName.Text, Email.Text, DeliveryAdd.Text);

                    updateStatus.Text = "Your account profile has been updated.";
                }
                catch
                {
                    updateStatus.Text = "An error occurred. Please try again.";
                }
            }
        }

        protected void ChangeProfPicButton_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                if (FileUpload1.FileName.Trim().Length > 100)
                {
                    updateStatus.Text = "Please use a shorter file name.";
                }
                else
                {
                    try
                    {
                        string username = (HttpContext.Current.User.Identity.Name);
                        updateStatus.Text = UserBusinsessLayer.saveProfilePic(FileUpload1.FileName, Server.MapPath("ProfilePic//" + FileUpload1.FileName), FileUpload1.PostedFile, username);

                    }
                    catch (Exception)
                    {
                        updateStatus.Text = "File could not be uploaded.";
                    }
                }
            }        
        }
    }
}
